package com.example.miniappssm.controller;

import com.example.miniappssm.bean.Admin;
import com.example.miniappssm.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
public class AdminController {

    @Autowired
    AdminService adminService;

    @RequestMapping("/showAllAdmin")
    public List<Admin> showAllAdmin() {
        return adminService.showAllAdmin();
    }

    @RequestMapping("/showAdmin")
    public Admin showAdmin(@RequestParam("openid") String openid) {
        return adminService.showAdminByOpenid(openid);
    }

    @RequestMapping("/checkIsInAdmins")
    public boolean checkIsInAdmins(@RequestParam("openid") String openid) {
        return adminService.openidIsInAdmins(openid);
    }

    @RequestMapping("/removeAdmin")
    public void removeAdmin(@RequestParam("openid") String openid) {
        adminService.removeAdminByOpenid(openid);
    }

    @PostMapping("/addAdmin")
    public void addAdmin(Admin admin, @RequestParam("code") String code) {
        if (code.equals("LJjsVP1RlcZzSRWM")) {
            adminService.addAdmin(admin);
        }
    }

    @PostMapping("/updateAdmin")
    public void updateAdmin(Admin admin) {
        adminService.updateAdmin(admin);
    }
}
